package com.rbac.api.mybatis.sqlProvider;


import com.rbac.api.constant.AdminConstant;
import com.rbac.api.controller.request.PostMenusListRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

public class AdminMenusSqlProvider {




    ///获取菜单列表数据条数
    public String getMenusListCountSelective(@Param("paramRequest") PostMenusListRequest menusListRequest){
        ///基础SQL
        SQL sql = new SQL();
        sql.SELECT("count(1)");
        sql.FROM("admin_menus");

        ///
        return sql.toString()+" limit 1 ";
    }
    ///获取菜单列表数据list
    public String getMenusListDataSelective(
            @Param("paramRequest") PostMenusListRequest menusListRequest,
            @Param("StartRow") Integer StartRow,
            @Param("pageSize") Integer pageSize
    ){
        ///基础SQL
        SQL sql = new SQL();
        sql.SELECT("id,menu_no,parent_no,menu_name,menu_address,menu_sort,menu_status");
        sql.FROM("admin_menus");

        ///不读取初始数据
        sql.WHERE("parent_no != '-1' ");

        ///排序
        sql.ORDER_BY("menu_status desc,menu_sort DESC ");


        ///
        return sql.toString() + " limit #{StartRow,jdbcType=INTEGER} , #{pageSize,jdbcType=INTEGER}";
    }



    ///根据管理员编号获取拥有的菜单list
    public String getHaveMenusListUserNoSelective(@Param("userNo") String userNo){
        if(StringUtils.isEmpty(userNo)){userNo="''";}
        ///基础SQL
        SQL sql = new SQL();
        sql.SELECT("aru.user_no,aru.role_no,am.menu_no,am.parent_no,am.menu_address,am.menu_name,am.menu_sort ");
        sql.FROM("admin_roles_users AS aru ");
        sql.LEFT_OUTER_JOIN("admin_roles_menus AS arm ON aru.role_no = arm.role_no ");
        sql.LEFT_OUTER_JOIN("admin_menus AS am ON arm.menu_no = am.menu_no ");

        ///如果不是超级管理员则不读取初始数据
        if (!(userNo.equals(AdminConstant.SUPER_ADMINISTRATOR_NUMBER_VALUE))){ sql.WHERE("am.parent_no != '-1' ");}

        sql.WHERE("aru.user_no = #{userNo,jdbcType=VARCHAR} ");
        sql.WHERE("am.menu_status = '2' ");

        ///排序
        sql.ORDER_BY("am.menu_sort DESC ");


        ///
        return sql.toString() + " limit #{StartRow,jdbcType=INTEGER} , #{pageSize,jdbcType=INTEGER}";
    }






    ///根据访问路径获取路径id
    public String selectMenuNoByMenuAddressSelective(@Param("path") String path){
        if(StringUtils.isEmpty(path)){path="''";}
        //SQL
        return "select\n"+
                "menu_no\n"+
                "from admin_menus\n"+
                "where menu_address = #{path,jdbcType=VARCHAR}\n"+
                "and `menu_status`='2' limit 1";


    }




    ///根据管理员编号和菜单编号count是否拥有此地址权限
    public String checkThisAddressPowerSelective(@Param("userNo") String userNo,@Param("menuNo") String menuNo){
        if(StringUtils.isEmpty(userNo)){userNo="''";}
        if(StringUtils.isEmpty(menuNo)){menuNo="''";}
        //SQL
        return "SELECT\n"+
                "COUNT(1)\n"+
                "FROM admin_roles_users AS aru\n"+
                "LEFT JOIN admin_roles_menus AS arm ON aru.role_no = arm.role_no\n"+
                "LEFT JOIN admin_menus AS am ON arm.menu_no = am.menu_no\n"+
                "WHERE aru.user_no = #{userNo,jdbcType=VARCHAR}\n"+
                "AND arm.menu_no = #{menuNo,jdbcType=VARCHAR}\n"+
                "AND am.menu_status = '2' limit 1";


    }








}